//
//  IMBottomBubbleView.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/30.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "IMBottomBubbleView.h"

@interface IMBottomBubbleView ()



@end

@implementation IMBottomBubbleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.bubble];
    [self addSubview:self.bubbleClose];
    
    [self.bubble mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.bubbleClose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bubble.mas_right).offset(-2);
        make.top.mas_equalTo(self.bubble.mas_top).offset(-10);
    }];
    
    [self layoutIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.bubble pp_clipCorners:UIRectCornerAllCorners radius:self.bubble.frame.size.height/2];
    });
}

- (void)hideBubble {
    if (self.IMBottomBubbleViewBlock) {
        self.IMBottomBubbleViewBlock(BubbleActionType_close);
    }
}

- (void)handleAction {
    if (self.IMBottomBubbleViewBlock) {
        self.IMBottomBubbleViewBlock(BubbleActionType_jump);
    }
}

- (UIButton *)bubble {
    if (!_bubble) {
        _bubble =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_bubble setBackgroundImage:[UIImage imageNamed:@"zixun_bg"] forState:UIControlStateNormal];
        [_bubble setImage:[UIImage imageNamed:@"zixun_gift"] forState:UIControlStateNormal];
        [_bubble setTitle:@"本门新推出爆款套餐，欢迎咨询" forState:UIControlStateNormal];
        _bubble.titleLabel.font = [UIFont systemFontOfSize:12];
        [_bubble setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _bubble.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        _bubble.imageEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 0);
        [_bubble addTarget:self action:@selector(handleAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bubble;
}

- (UIButton *)bubbleClose {
    if (!_bubbleClose) {
        _bubbleClose =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_bubbleClose setImage:[UIImage imageNamed:@"public_close_small"] forState:UIControlStateNormal];
        _bubbleClose.titleLabel.font = [UIFont systemFontOfSize:12];
        [_bubbleClose setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_bubbleClose addTarget:self action:@selector(hideBubble) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bubbleClose;
}

@end
